import 'package:appflutterhomeschool/model/get_chat_massage_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/colors.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/ui/widgets/image_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/utils/utils.dart';
import 'package:appflutterhomeschool/view_model/notice/discuss_view_model.dart';
import 'package:appflutterhomeschool/view_model/notice/send_out_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';

///用户： ninetailedfox
///时间： 2020/6/17
///邮箱： 1037438704@qq.com
///功能： 讨论页面
class DiscussPage extends StatelessWidget {
  final int notificationId;

  DiscussPage({this.notificationId});

  @override
  Widget build(BuildContext context) {
    return ProviderWidget<DiscussViewModel>(
      model: DiscussViewModel(context, notificationId),
      onModelReady: (value) => value.initView(),
      builder: (context, viewModel, child) {
        return Scaffold(
          backgroundColor: Colors.grey[100],
          appBar: MyAppBar2(
            title: viewModel?.titleText ?? "",
            rightIcon: Visibility(
              visible: viewModel.myAppViewModel.identity,
              child: GestureDetector(
                child: Image.asset(Utils.getImgPath("taolun_jieshu")),
                onTap: () {
                  Fluttertoast.showToast(msg: "结束成功");
                  Navigator.pop(context);
                },
              ),
            ),
          ),
          body: viewModel.listGetChatMassage == null
              ? Center(
                  child: CircularProgressIndicator(
                  backgroundColor: Colors.blue,
                ))
              : Stack(
                  children: <Widget>[
                    Positioned(
                      top: 0,
                      left: 0,
                      right: 0,
                      child: Container(
                        height: ScreenUtil().setWidth(80),
                        color: Colours.gray_f7,
                        alignment: Alignment.center,
                        child: Text.rich(TextSpan(children: [
                          TextSpan(
                              text: "3人未参加\t》\t\t·\t\t",
                              style: TextStyles.textBlue30),
                          TextSpan(
                              text: "全部已读\t》", style: TextStyles.textGray30),
                        ])),
                      ),
                    ),
                    Positioned(
                      top: ScreenUtil().setWidth(80),
                      left: 0,
                      right: 0,
                      bottom: ScreenUtil().setWidth(134),
                      child: ListView.builder(
                          shrinkWrap: true,
                          itemCount: viewModel.listGetChatMassage.length,
                          itemBuilder: itemChatWidget),
                    ),
                    Positioned(
                      bottom: 0,
                      left: 0,
                      right: 0,
                      child: SendOutWidget(),
                    )
                  ],
                ),
        );
      },
    );
  }

  Widget itemChatWidget(context, index) {
    DiscussViewModel discussViewModel = Provider.of(context);
    GetChatMassageModel getChatMassageModel =
        discussViewModel.listGetChatMassage[index];
    GetChatMassageModel getChatMassageModel2;
    if (index != 0) {
      getChatMassageModel2 = discussViewModel.listGetChatMassage[index];
    }

    return Column(
      children: <Widget>[
        Visibility(
          visible: index == 0
              ? true
              : (getChatMassageModel.monthDay == getChatMassageModel2.monthDay
                  ? false
                  : true),
          child: ShadowWidget(
            externalPadding: EdgeInsets.only(
                left: ScreenUtil().setWidth(35),
                right: ScreenUtil().setWidth(35),
                top: ScreenUtil().setWidth(35)),
            withinPadding: EdgeInsets.fromLTRB(10, 3, 10, 5),
            childWidget: Text(
              getChatMassageModel?.monthDay ?? "",
              style: TextStyles.textGrayA231,
            ),
          ),
        ),
        SizedBox(height: ScreenUtil().setWidth(90)),
        getChatMassageModel.isSelf != 0
            ? leftWidget(getChatMassageModel)
            : rightWidget(getChatMassageModel),
      ],
    );
  }

  Widget leftWidget(GetChatMassageModel getChatMassageModel) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(
              left: ScreenUtil().setWidth(35),
              right: ScreenUtil().setWidth(35)),
          child: ImageWidget(
            imageUrl: getChatMassageModel?.senderImg,
          ),
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                child: Row(
                  children: [
                    Text(getChatMassageModel?.sender ?? "",
                        style: TextStyles.textDark30),
                    Text(getChatMassageModel?.timeDisplay ?? "",
                        style: TextStyles.textGray30)
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                    top: ScreenUtil().setWidth(20),
                    right: ScreenUtil().setWidth(139)),
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil().setWidth(35),
                    ScreenUtil().setWidth(51),
                    ScreenUtil().setWidth(35),
                    ScreenUtil().setWidth(51)),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                child: Text(
                  getChatMassageModel?.msg ?? "",
                  style: TextStyles.textDark37,
                ),
              )
            ],
          ),
        ),
      ],
    );
  }

  Widget rightWidget(GetChatMassageModel getChatMassageModel) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Container(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text(getChatMassageModel?.timeDisplay ?? "",
                        style: TextStyles.textGray30),
                    Text(getChatMassageModel?.sender ?? "",
                        style: TextStyles.textDark30),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                    top: ScreenUtil().setWidth(20),
                    left: ScreenUtil().setWidth(139)),
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil().setWidth(35),
                    ScreenUtil().setWidth(51),
                    ScreenUtil().setWidth(35),
                    ScreenUtil().setWidth(51)),
                decoration: BoxDecoration(
                    color: Colors.blue,
                    borderRadius: BorderRadius.circular(10)),
                child: Text(
                  getChatMassageModel?.msg ?? "",
                  style: TextStyles.textDark37,
                ),
              )
            ],
          ),
        ),
        Container(
          margin: EdgeInsets.only(
              left: ScreenUtil().setWidth(35),
              right: ScreenUtil().setWidth(35)),
          child: ImageWidget(
            imageUrl: getChatMassageModel?.senderImg,
          ),
        ),
      ],
    );
  }
}
